﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTKERP.DAL;
using BTKERP.Model;
using BTKERP.Model.Web;

namespace BTKERP.BLL.Inventory
{
    public class InventoryActivityMgt
    {
        private static readonly RequisitionMasterDAL reqMasterDAL = DataContextHelper.GetDbContect<RequisitionMasterDAL>();
        private static readonly RequisitionDetailDAL reqDetailDAL = DataContextHelper.GetDbContect<RequisitionDetailDAL>();
        private static readonly RequisitionDeliveryMasterDAL doMasterDAL = DataContextHelper.GetDbContect<RequisitionDeliveryMasterDAL>();
        private static readonly GRNMasterDAL grnMasterDAL = DataContextHelper.GetDbContect<GRNMasterDAL>();
        private static readonly GRNDetailDAL grnDetailDAL = DataContextHelper.GetDbContect<GRNDetailDAL>();

        #region Requistition Management

        public static List<RequisitionMaster> GetRequisitionMasters()
        {
            return reqMasterDAL.GetAll();
        }

        public static RequisitionMaster GetRequisitionMasterById(Guid id)
        {
            return reqMasterDAL.GetById(id);
        }

        public static bool DeleteRequisitionMaster(Guid id)
        {
            return reqMasterDAL.DeleteById(id);
        }

        public static string AddNewRequisition(Guid fromHouse, Guid toHouse, string code, DateTime requisitionDate, string requestBy, string fullFillBy, string createBy, DateTime deliveryDate)
        {
            return reqMasterDAL.AddNewRequisition(fromHouse, toHouse, code, requisitionDate, requestBy, fullFillBy, createBy, deliveryDate);
        }

        public static RequisitionMaster UpdateRequisitionMaster(Guid paramId, Guid paramToWarehouseId, string paramRequisitionCode,
                                            DateTime paramRequisitionDate,string requisitionBy, DateTime paramDeliveryDate, decimal paramRequisitionTotal,
                                            int paramStatus, string paramUpdatedBy)
        {
            return reqMasterDAL.UpdateById(paramId,  paramToWarehouseId,  paramRequisitionCode, paramRequisitionDate,requisitionBy, paramDeliveryDate,  paramRequisitionTotal,paramStatus,   paramUpdatedBy);
        }

        public static List<RequisitionDetail> GetRequisitionDetails()
        {
            return reqDetailDAL.GetAll();
        }

        public static List<RequisitionDetail> GetRequisitionDetailByMasterId(Guid id)
        {
            return reqDetailDAL.GetByMasterId(id);
        }

        public static bool DeleteRequisitionDetail(Guid id,Guid productId)
        {
            return reqDetailDAL.DeleteById(id,productId);
        }

        public static RequisitionDetail UpdateRequisitionDetail(Guid paramResuisitionId, Guid paramProductId, Guid paramUnitId, decimal paramUnitPrice,
                                            decimal paramRequestedQty, decimal paramRequstedTotal, decimal paramApprovedQty, decimal paramApprovedTotal,
                                            decimal paramDeliveredQty, string paramStatus, string paramUpdatedBy)
        {
            return reqDetailDAL.UpdateById(paramResuisitionId, paramProductId, paramUnitId,	 paramUnitPrice,
                                             paramRequestedQty,  paramRequstedTotal,  paramApprovedQty,  paramApprovedTotal,
                                             paramDeliveredQty,  paramStatus,  paramUpdatedBy);
        }



        public static void UpdateRequisitionDetailProcess(Guid resuisitionId, Guid productId, Guid unitId, int requestQty, string updateBy)
        {
            reqDetailDAL.UpdateProcess(resuisitionId, productId, unitId, requestQty, updateBy);
        }
        #endregion


        public static List<RequisitionMaster> SearchRequisitionMaster(string houseId, string reqCode, string reqDate, string user, int status)
        {
            return reqMasterDAL.SearchMaster(houseId, reqCode, reqDate, user, status);
        }

        public static bool VoidMaster(string id, string name)
        {
            return reqMasterDAL.ChangeStatus(new Guid(id), name, 4);
        }

        public static bool RejectMaster(string id, string name)
        {
            return reqMasterDAL.ChangeStatus(new Guid(id), name, 3);
        }

        public static string ApproveMaster(string masterId, string name)
        {
            return reqMasterDAL.ApproveMaster(new Guid(masterId), name);
        }


        #region GRN Management
        public static List<GRNMaster> GetGRNMasters()
        {
            return grnMasterDAL.GetAll();
        }

        public static GRNMaster GetGRNMasterById(Guid id)
        {
            return grnMasterDAL.GetById(id);
        }

        public static GRNMaster AddGRNMaster(string paramGRNCode, DateTime paramGRNDate, Guid paramSupplierId, Guid paramReceivedWarehouseId, string paramRemark,
                                   int paramStatus, string paramCreatedBy)
        {
            return grnMasterDAL.Add( paramGRNCode,  paramGRNDate,  paramSupplierId,  paramReceivedWarehouseId,  paramRemark,
                                    paramStatus,  paramCreatedBy);
        }

        public static GRNMaster UpdateGRNMaster(Guid paramId, string paramGRNCode, DateTime paramGRNDate, Guid paramSupplierId, Guid paramReceivedWarehouseId, string paramRemark,
                                    decimal paramReceivedQuantity, decimal paramReceivedValue, int paramStatus, bool paramIsConsignment, decimal paramConsignmentComission,
                                    string paramUpdatedBy)
        {
            return grnMasterDAL.UpdateById(paramId,  paramGRNCode,  paramGRNDate,  paramSupplierId,  paramReceivedWarehouseId,  paramRemark,
                                     paramReceivedQuantity,  paramReceivedValue,  paramStatus,  paramIsConsignment,  paramConsignmentComission,
                                     paramUpdatedBy);
        }


        public static List<RequisitionDeliveryMaster> SearchDOMaster(string houseId, string requId, string doNo, string requeBy, int status)
        {
            return doMasterDAL.search(houseId, requId, doNo, requeBy, status);
        }

        public static List<ItemCountModel> GetRequisitionCountByStatus()
         {
             return reqMasterDAL.GetRequisitionCountByStatus();
         }

        public static List<ItemCountModel> GetDOMasterCountByStatus()
        {
            return doMasterDAL.GetDOMasterCountByStatus();
        }


        public static List<GRNDetail> GetGRNDetails()
        {
            return grnDetailDAL.GetAll();
        }

        public static List<GRNDetail> GetGRNDetailByMasterId(Guid grnMasterId)
        {
            return grnDetailDAL.GetByMasterId(grnMasterId);
        }

        public static GRNDetail AddGRNDetail(Guid paramGRNMasterId, Guid paramProductId, Guid paramUnitId, decimal paramQuantity,
                                    decimal paramUnitCost, decimal paramTotal, decimal paramSoldQty, bool paramIsConsignment,
                                    decimal paramConsignmentCommission, string paramRemark, string paramCreatedBy)
        {
            return grnDetailDAL.Add(paramGRNMasterId,  paramProductId,  paramUnitId,  paramQuantity,
                                     paramUnitCost,  paramTotal,  paramSoldQty,  paramIsConsignment,
                                     paramConsignmentCommission,  paramRemark,  paramCreatedBy);
        }
		

        public static GRNDetail UpdateGRNDetail(Guid paramGRNMasterId, Guid paramProductId, Guid paramUnitId, decimal paramQuantity,
                                    decimal paramUnitCost, decimal paramTotal, decimal paramSoldQty, bool paramIsConsignment,
                                    decimal paramConsignmentCommission, string paramRemark, string paramUpdatedBy)
        {
           return grnDetailDAL.UpdateById(paramGRNMasterId,  paramProductId,  paramUnitId,  paramQuantity,
                                     paramUnitCost,  paramTotal,  paramSoldQty,  paramIsConsignment,
                                     paramConsignmentCommission,  paramRemark,  paramUpdatedBy);
        }

        public static bool PhysicalDelteGRNDetail(Guid paramMasterId, Guid paramProductId)
        {
            return grnDetailDAL.PhysicalDeleteById(paramMasterId, paramProductId);
        }

        public static bool PostGRN(Guid paramMasterId, Guid warehouseID)
        {
           // return grnMasterDAL.PostGRN(paramMasterId, warehouseID);
            return true;
        }
		
	}
        #endregion
    

}
